<template>
  <div class="bodyList">
    <ItemBox :items="items" v-for="(items,index) in itemList" :key="index"/>
   
   <div class="pageGroup">
       <div class="btn btn-Left" @click="btnLeft">&lt;上一页</div>
       <div class="btn btn-right" @click="btnRight">下一页&gt;</div>
   </div>

    <div class="loading" v-show="isShow">
         <img src="../assets/images/logo.png" style="width:100px">
         <br>
        <span>加载中......</span> 
    </div>
  </div>
</template>

<script>
import ItemBox from './ItemBox'
export default {
    name:'BodyList',
    components:{ItemBox},
    data() {
        return {
            itemList:[],
            page:1,
            isShow:false
        }
    },
    created() {
       this.getData()
    },
    methods: {
        btnLeft(){
            if(this.page<=1){
                this.page = 1
                alert('这是第一页，不能再向前了!')
                return
            }
            this.isShow = true
            this.page--
            this.getData()
            this.backTop()
        },
        backTop(){
            if(document.body.scrollTop){
                document.body.scrollTop = 0
            }else{
                document.documentElement.scrollTop = 0
            }
        },
        btnRight(){
            if(this.itemList.length<9 || this.itemList.length === 0){
                alert('这是最后一页了，不能再往后了！')
                return
            }
            this.isShow = true
            this.page++
            this.getData()
            this.backTop()
        },
        getData(){
             this.$axios({
                url:'https://ku.qingnian8.com/dataApi/blog/showBlog.php',
                params:{
                    page:this.page
                }
            }).then(res =>{
                this.itemList = res.data
                this.isShow = false
            })
        }
    },
    
}
</script>

<style scoped lang="less">
.bodyList{
    padding:60px 30px;
}
.pageGroup{
    padding: 30px;
    display: flex;
    justify-content: center;
    align-items: center;
    .btn{
        border:1px solid #41B883;
        padding: 10px 20px;
        color:#41B883 ;
        font-size: 18px;
        margin: 0 10px;
        cursor: pointer;
        transition: 0.3s;
        &:hover{
            background-color: #41B883;
            color: white;
        }
    }
}
.loading{
    left: 0;
    top: 0;
    position: fixed;
    background-color: rgba(0,0,0,0.3);
    color: white;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    z-index: 100;
    span{
        z-index: 101;
        font-size: 30px;
        margin-top: 150px;
    }
}
</style>